package cn.edu.gdut.deepcode.controller;


import cn.edu.gdut.deepcode.domain.po.Result;
import cn.edu.gdut.deepcode.service.ICanvasSnapshotService;
import cn.edu.gdut.deepcode.utils.CommonUtils;
import cn.edu.gdut.deepcode.context.UserContext;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.edu.gdut.deepcode.constant.AbnormalInfoConstant.CD_EI_2;
import static cn.edu.gdut.deepcode.constant.ReqRespConstant.AUTHORIZATION_HEADER_NAME;

/**
 * <p>
 * 画布快照表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-04-11
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/canvas-snapshots")
public class CanvasSnapshotController {
    private final ICanvasSnapshotService canvasSnapshotService;

    @ApiOperation(value = "直接存储快照接口",notes = "响应体中的data属性为null")
    @ApiImplicitParam(
            name = AUTHORIZATION_HEADER_NAME,
            value = "JWT 令牌",
            required = true,
            dataType = "string",
            paramType = "header"
    )
    @PostMapping("/direct/{canvasId}")
    public Result<?> directSaveSnapshot(
            @ApiParam("画布id") @PathVariable Long canvasId,
            @RequestBody String canvasData
                                        ){
        // 画布数据异常
        if (StrUtil.isBlank(canvasData)){
            CommonUtils.recordErrorInfoAndThrowException(
                    CD_EI_2,
                    List.of(UserContext.getUser(),canvasData,canvasData)
            );
        }

        return canvasSnapshotService.directSaveSnapshot(canvasId,canvasData);
    }
    @ApiOperation(value = "根据画布id获取快照接口",notes = "响应体中的data属性为画布快照,也就是json格式的画布信息")
    @ApiImplicitParam(
            name = AUTHORIZATION_HEADER_NAME,
            value = "JWT 令牌",
            required = true,
            dataType = "string",
            paramType = "header"
    )
    @GetMapping("/{canvasId}")
    public Result<String> getSnapshotById(
            @ApiParam("画布id")
            @PathVariable Long canvasId){
        return canvasSnapshotService.getSnapshotById(canvasId);
    }



}
